package com.itheima.spring.context;

import com.itheima.spring.beans.BeanDefinitionRegistry;
import com.itheima.spring.beans.BeanFactory;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 加载xml配置文件得到Resource,
 * 然后bean定义读取器BeanDefinitionReader从Resource里面加载bean定义注册到bean工厂
 */
public class XmlApplicationContext extends AbstractApplicationContext {


    //加载xml配置文件的产物Resource
    private List<Resource> resources;

    //bean定义读取器
    private BeanDefinitionReader reader;

    public XmlApplicationContext(String... location)throws Throwable {
        super();
        //加载resource
        load(location);
        //bean定义读取器从Resource里面加载bean定义
        this.reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.beanFactory);
        reader.loadBeanDefintions((Resource[]) resources.toArray());
    }

    /**
     * 根据不同的字符串创建不同的Resource对象
     * @param location
     * @return
     * @throws IOException
     */
    @Override
    public Resource getResource(String location) throws IOException {
        if(StringUtils.isNotBlank(location)){
            if(location.startsWith(Resource.CLASS_PATH_PREFIX)){
                return new ClassPathResource(location.substring(Resource.CLASS_PATH_PREFIX.length()));
            }else if(location.startsWith(Resource.File_SYSTEM_PREFIX)){
                return new FileSystemResource(location.substring(Resource.File_SYSTEM_PREFIX.length()));
            }else {
                return new UrlResource(location);
            }
        }
        return null;
    }

    /**
     * 加载不同的Resource
     */
    private void load(String... location) throws IOException{
        if(resources == null){
            resources = new ArrayList<>();
        }
        //完成加载,创建好Resource
        if(location !=null && location.length>0){
            for (String lo:location){
                Resource resource = this.getResource(lo);
                if(resource != null){
                    resources.add(resource);
                }
            }
        }
    }
}
